<script>
  ;(function() {
    var config = window.whistleMenuConfig;
    var list = [];
    var networkListeners = [];
    var rulesListeners = [];
    var valuesListeners = [];
    var handleOnLoad = function() {
      timer = null;
      list.forEach(emit);
      list = null;
    };
    var timer = setTimeout(handleOnLoad, 12000);

    function execListeners(listeners, options) {
      listeners.forEach(function(fn) {
        try {
          fn(options)
        } catch (e) {
          console.error(e);
        }
      });
    }

    function emit(options) {
      switch(options.type) {
        case 'rules':
          execListeners(rulesListeners, options);
          break;
        case 'values':
          execListeners(valuesListeners, options);
          break;
        default:
          execListeners(networkListeners, options);
      }
    }
    window.onWhistleContextMenuClick = function(options) {
      if (list) {
        list.push(options);
      } else {
        emit(options);
      }
    };
    var delayOnLoad = function() {
      if (timer) {
        setTimeout(handleOnLoad, 100);
        clearTimeout(timer);
        timer = null;
      }
    };
    window.onload = delayOnLoad;
    try {
      window.addEventListener('load', delayOnLoad);
    } catch (e) {}
    var toast = {};
    var addNetworkListener = function(l) {
      if (typeof l === 'function' && networkListeners.indexOf(l) === -1) {
        networkListeners.push(l);
      }
    };
    var removeNetworkListener = function(l) {
      l = networkListeners.indexOf(l);
      if (l !== -1) {
        networkListeners.splice(l, 1);
      }
    };
    var removeAllNetworkListeners = function() {
      networkListeners = [];
    };
    var addRulesListener = function(l) {
      if (typeof l === 'function' && rulesListeners.indexOf(l) === -1) {
        rulesListeners.push(l);
      }
    };
    var removeRulesListener = function(l) {
      l = rulesListeners.indexOf(l);
      if (l !== -1) {
        rulesListeners.splice(l, 1);
      }
    };
    var removeAllRulesListeners = function() {
      rulesListeners = [];
    };
    var addValuesListener = function(l) {
      if (typeof l === 'function' && valuesListeners.indexOf(l) === -1) {
        valuesListeners.push(l);
      }
    };
    var removeValuesListener = function(l) {
      l = valuesListeners.indexOf(l);
      if (l !== -1) {
        valuesListeners.splice(l, 1);
      }
    };
    var removeAllValuesListeners = function() {
      valuesListeners = [];
    };

    function on(type, l) {
      if (typeof l !== 'function') {
        return;
      }
      switch(type) {
        case 'network':
          return addNetworkListener(l);
        case 'rules':
          return addRulesListener(l);
        case 'values':
          return addValuesListener(l);
      }
    }

    function off(type, l) {
      switch(type) {
        case 'network':
          if (l) {
            removeNetworkListener(l);
          } else {
            removeAllNetworkListeners();
          }
          return;
        case 'rules':
          if (l) {
            removeRulesListener(l);
          } else {
            removeAllRulesListeners();
          }
          return;
        case 'values':
          if (l) {
            removeValuesListener(l);
          } else {
            removeAllValuesListeners();
          }
          return;
      }
    }
    
    var whistleBridge = {
      config: config,
      toast: toast,
      on: on,
      off: off,
      addNetworkListener: addNetworkListener,
      removeNetworkListener: removeNetworkListener,
      removeAllNetworkListeners: removeAllNetworkListeners,
      addRulesListener: addRulesListener,
      removeRulesListener: removeRulesListener,
      removeAllRulesListeners: removeAllRulesListeners,
      addValuesListener: addValuesListener,
      removeValuesListener: removeValuesListener,
      removeAllValuesListeners: removeAllValuesListeners
    };
    try {
      window.initWhistleBridge = function(options) {
        window.initWhistleBridge = function() {};
        Object.keys(options.msgBox).forEach(function(name) {
          toast[name] = options.msgBox[name];
        });
        whistleBridge.getSelectedSessionList = options.getSelectedSessionList;
        whistleBridge.getActiveSession = whistleBridge.getSession = whistleBridge.getSelectedSession = options.getActiveSession;
        whistleBridge.showOption = options.showOption;
        whistleBridge.hideOption = options.hideOption;
        whistleBridge.updateUI = options.updateUI;
        whistleBridge.copyText = options.copyText;
        whistleBridge.pageId = options.pageId;
        whistleBridge.compose = options.compose;
        whistleBridge.decodeBase64 = options.decodeBase64;
        whistleBridge.importSessions = options.importSessions;
        whistleBridge.exportSessions = options.exportSessions;
        whistleBridge.importMockData = options.importMockData;
        whistleBridge.download = options.download;
        whistleBridge.setNetworkSettings = options.setNetworkSettings;
        whistleBridge.setComposerData = options.setComposerData;
        whistleBridge.request = options.request;
        whistleBridge.createRequest = options.createRequest;
        whistleBridge.showModal = options.showModal;
        whistleBridge.createModal = options.createModal;
        whistleBridge.importRules = options.importRules;
        whistleBridge.importValues = options.importValues;
        whistleBridge.getServerInfo = options.getServerInfo;
        whistleBridge.alert = options.alert;
        whistleBridge.confirm = options.confirm;
        whistleBridge.syncData = options.syncData;
        whistleBridge.syncRules = options.syncRules;
        whistleBridge.syncValues = options.syncValues;
      };
      window.parent.onPluginContextMenuReady(window);
    } catch (e) {}
    window.whistleBridge = whistleBridge;
  })();
</script>
